package com.r.pan.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.r.pan.config.AppConfig;
import com.r.pan.entity.dto.LoginFormDTO;
import com.r.pan.entity.dto.ResetPasswordFormDTO;
import com.r.pan.entity.dto.SpaceInfoDTO;
import com.r.pan.entity.po.UserInfo;
import com.r.pan.entity.vo.LoginInfoVO;
import com.r.pan.enums.UserStatus;
import com.r.pan.exception.BadRequestException;
import com.r.pan.exception.IllegalException;
import com.r.pan.mapper.UserInfoMapper;
import com.r.pan.service.IEmailCodeService;
import com.r.pan.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.r.pan.utils.BeanUtils;
import com.r.pan.utils.RedisUtils;
import com.r.pan.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author r-pocky
 * @since 2024-12-15
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Resource
    private AppConfig appConfig;
    @Resource
    private RedisUtils redisUtils;

    @Resource
    private IEmailCodeService emailCodeService;

    @Override
    public LoginInfoVO login(LoginFormDTO loginFormDTO) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getEmail, loginFormDTO.getEmail());
        UserInfo userInfo = getOne(queryWrapper);
        if (null == userInfo || !userInfo.getPassword().equals(loginFormDTO.getPassword())) {
            throw new BadRequestException("账号或密码错误");
        }
        if (UserStatus.DISABLED.equals(userInfo.getStatus())) {
            throw new IllegalException("该账号已被禁用");
        }
        // 更新最后登陆时间
        userInfo.setLastLoginTime(LocalDateTime.now());
        // 构造登录信息
        LoginInfoVO loginInfoVO = BeanUtils.copyBean(userInfo, LoginInfoVO.class);
        // 判断是否管理员账号
        String[] adminEmails = appConfig.getAdminEmails();
        loginInfoVO.setIsAdmin(ArrayUtil.contains(adminEmails, userInfo.getEmail()));
        // todo 初始化loginInfoVO的用户头像信息
        // 获取用户空间使用信息，保存到redis缓存中
        SpaceInfoDTO spaceInfoDTO = BeanUtils.copyBean(userInfo, SpaceInfoDTO.class);
        redisUtils.setSpaceInfo(userInfo.getUserId(), spaceInfoDTO);
        return loginInfoVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void resetPassword(ResetPasswordFormDTO resetPasswordFormDTO) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getEmail, resetPasswordFormDTO.getEmail());
        UserInfo userInfo = getOne(queryWrapper);
        if (null == userInfo) {
            throw new BadRequestException("邮箱账号不存在");
        }
        // 校验邮箱验证码
        emailCodeService.checkEmailCode(userInfo.getEmail(), resetPasswordFormDTO.getEmailCode());
        // 更新用户密码
        userInfo.setPassword(StringUtils.encodeMD5(resetPasswordFormDTO.getPassword()));
        updateById(userInfo);
    }
}
